package cn.tedu.jsd2207csmall.passport.repo.impl;


import cn.tedu.jsd2207csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.jsd2207csmall.passport.pojo.vo.AdminStandardVO;
import cn.tedu.jsd2207csmall.passport.repo.IAdminRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Repository
public class AdminRedisRepository implements IAdminRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    public AdminRedisRepository() {
        log.debug("创建处理缓存的数据访问对象:AdminRedisRepository");
    }

    @Override
    public void save(AdminStandardVO adminStandardVO) {
        String key=ADMIN_ITEM_KEY_PREFIX+adminStandardVO.getId();
        redisTemplate.opsForSet().add(ADMIN_ITEM_KEYS_KEY,key);
         redisTemplate.opsForValue().set(key,adminStandardVO);
    }

    @Override
    public void save(List<AdminListItemVO> admins) {
        String key=ADMIN_LIST_KEY;
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        for (AdminListItemVO admin : admins) {
            opsForList.rightPush(key,admin);
        }
    }

    @Override
    public Long deleteAll() {
        Set<Serializable> members = redisTemplate.opsForSet().members(ADMIN_ITEM_KEYS_KEY);
        Set<String> keys=new HashSet<>();
        for (Serializable member : members) {
            keys.add((String) member);
        }
        keys.add(ADMIN_LIST_KEY);
        keys.add(ADMIN_ITEM_KEYS_KEY);
        return redisTemplate.delete(keys);
    }

    @Override
    public AdminStandardVO get(Long id) {
        String key=ADMIN_ITEM_KEY_PREFIX+id;
        Serializable serializable = redisTemplate.opsForValue().get(key);
        AdminStandardVO adminStandardVO=null;
        if (serializable!=null){
            if (serializable instanceof AdminStandardVO){
                adminStandardVO= (AdminStandardVO) serializable;
            }
        }
        return adminStandardVO;
    }

    @Override
    public List<AdminListItemVO> getList() {

        long start=0;
        long end=-1;
        return getList(start,end);

    }

    @Override
    public List<AdminListItemVO> getList(long start, long end) {
        String key=ADMIN_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> range = ops.range(key, start, end);
        List<AdminListItemVO> admins=new ArrayList<>();
        for (Serializable serializable : range) {
            admins.add((AdminListItemVO) serializable);
        }
        return admins;
    }
}
